using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Reflection;
using System.Text;
using System.Web;
using Castle.ActiveRecord;
using NHibernate.Expression;

namespace WOODIFramework.UI.Model
{
    [ActiveRecord("Blog")]
    public class Blog : ActiveRecordBase<Blog>
    {
        #region Fields

        private int _id;
        private string _title;
        private string _subTitle;
        private string _body;
        private Category _category;
        private DateTime _dateAdded;

        #endregion

        #region Properties

        [PrimaryKey(PrimaryKeyType.Identity)]
        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }

        [Property]
        public string Title
        {
            get { return _title; }
            set { _title = value; }
        }

        [Property]
        public string SubTitle
        {
            get { return _subTitle; }
            set { _subTitle = value; }
        }

        [Property]
        public string Body
        {
            get { return _body; }
            set { _body = value; }
        }

        [Property]
        public DateTime DateAdded
        {
            get { return _dateAdded; }
            set { _dateAdded = value; }
        }

        [BelongsTo("CategoryId")]
        public Category BlogCategory
        {
            get { return _category; }
            set { _category = value; }
        }

        #endregion

        #region Constructors

        public Blog()
        {
        }

        private Blog(string Title)
        {
            this.Title = Title;
        }

        #endregion

        #region Public Methods

        #region Public Static Methods

        public static Blog CreateBlog()
        {
            return new Blog();
        }

        public static Blog[] FindAll()
        {
            return (Blog[])Blog.FindAll(typeof(Blog));
        }

        public static Blog CreateBlog(string Title)
        {
            return new Blog(Title);
        }
        
        public static Blog FindBlogByTitle(string Title)
        {
            ICriterion criteria = Expression.Eq("Title", Title);
            Blog blog = Blog.FindFirst(criteria);
            return blog;
        }

        public static Blog FindByID(object PrimaryKey)
        {
            Blog blog = Blog.FindBlogByID(Convert.ToInt32(PrimaryKey));
            return blog;
        }

        public static Blog FindBlogByID(int Id)
        {
            ICriterion criteria = Expression.Eq("Id", Id);
            Blog blog = Blog.FindFirst(criteria);
            return blog;
        }

        public static Blog[] FindBlogsByCategory(Object CategoryId)
        {
            int categoryId = Convert.ToInt32(CategoryId);
            Category category = Category.FindCategoryById(categoryId);
            
            Blog[] blogs = Blog.FindAllByProperty("BlogCategory", category);

            return blogs;
        }
        
        #endregion

        #endregion
    }
}
